package com.wafersystems.cloud.bo;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.wafersystems.cloud.bo.abstractbo.AbstractDoor;

/**
 * @Description: 开关量锁 康奈德
 * @author lihy
 * @date 2018年12月5日
 */
public class SwitchLocalDoor extends AbstractDoor {
  private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

  @Override
  public void open() {
    try {
      int devid = Integer.parseInt(getDeviceId()) + 63;
      String sendText = "000100000006FF0500" + devid + "ff00";
      byte[] order = new byte[12];
      for (int i = 0; i < 12; i++) {
        int parseInt = Integer.parseInt(sendText.substring(i * 2, i * 2 + 2), 16);
        order[i] = (byte) parseInt;
      }
      deviceController.send(order);
    } catch (NumberFormatException e) {
      e.printStackTrace();
    } finally {
      executor.schedule(new Runnable() {
        @Override
        public void run() {
          close();
          //read();
        }
      }, 2, TimeUnit.SECONDS);
    }
  }

  @Override
  public void close() {
    int devid = Integer.parseInt(getDeviceId()) + 63;
    String sendText = "000100000006FF0500" + devid + "0000";
    byte[] order = new byte[12];
    for (int i = 0; i < 12; i++) {
      int parseInt = Integer.parseInt(sendText.substring(i * 2, i * 2 + 2), 16);
      order[i] = (byte) parseInt;
    }
    deviceController.send(order);
  }
  
  private void read() {
    int devid = Integer.parseInt(getDeviceId()) + 63;
    String sendText = "000100000006FF020000c80004";
    byte[] order = new byte[12];
    for (int i = 0; i < 12; i++) {
      int parseInt = Integer.parseInt(sendText.substring(i * 2, i * 2 + 2), 16);
      order[i] = (byte) parseInt;
    }
    deviceController.send(order);
  }

}
